package com.xckj.ibox.view;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 底部菜单
 * @Date: 2023/6/13 17:58
 */
public class BottomMenuBar extends LinearLayout {

    private final List<BottomTabView> mTabView = new ArrayList<>();
    private int mCurrentPosition = 0;
    private OnTabSelectedListener mListener;


    public BottomMenuBar(Context context) {
        this(context, null);
        setOrientation(LinearLayout.HORIZONTAL);
    }

    public BottomMenuBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        setOrientation(LinearLayout.HORIZONTAL);
    }

    public BottomMenuBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.HORIZONTAL);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            if (view instanceof BottomTabView) {
                view.setOnClickListener(tabClickListener);
                mTabView.add((BottomTabView) view);
            }
        }
    }

    View.OnClickListener tabClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View tabView) {
            if (mListener == null) {
                return;
            }
            int pos = mTabView.indexOf(tabView);
            if (mListener.onIntercept(mCurrentPosition, pos)) {
                return;
            }
            mTabView.get(pos).startRotation();
            if (mCurrentPosition == pos && tabView.isSelected()) {
                mListener.onTabReselected(pos);
            } else {
                int preposition = mCurrentPosition;
                mCurrentPosition = pos;
                mListener.onTabSelected(pos, preposition);
                mListener.onTabUnselected(preposition);
                setSelect(pos);
            }
        }
    };


    private void setSelect(int position) {
        for (int i = 0; i < mTabView.size(); i++) {
            boolean select = i == position ? true : false;
            mTabView.get(i).setSelected(select);
        }
    }


    public void setOnTabSelectedListener(OnTabSelectedListener onTabSelectedListener) {
        mListener = onTabSelectedListener;
    }

    public BottomTabView getTab(int position) {
        return mTabView.get(position);
    }

    public void setCurrentItem(final int position) {
        post(new Runnable() {
            @Override
            public void run() {
                mTabView.get(position).performClick();
            }
        });
    }

    public int getCurrentItemPosition() {
        return mCurrentPosition;
    }


    public interface OnTabSelectedListener {
        void onTabSelected(int position, int prePosition);

        void onTabUnselected(int position);

        void onTabReselected(int position);

        boolean onIntercept(int prePosition, int newPosition);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        return new SavedState(superState, mCurrentPosition);
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());

        if (mCurrentPosition != ss.position) {
            mTabView.get(mCurrentPosition).setSelected(false);
            mTabView.get(ss.position).setSelected(true);
        }
        mCurrentPosition = ss.position;
    }

    static class SavedState extends View.BaseSavedState {
        private final int position;

        public SavedState(Parcel source) {
            super(source);
            position = source.readInt();
        }

        public SavedState(Parcelable superState, int position) {
            super(superState);
            this.position = position;
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(position);
        }

        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
            @Override
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            @Override
            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
}